//
//  CertificateRequest.m
//  baovn
//
//  Created by Phuong Dinh Le on 3/22/13.
//  Copyright (c) 2013. All rights reserved.
//

#import "CertificateRequest.h"
#import "AppInfo.h"

@implementation CertificateRequest

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSURL *url = [[connection currentRequest] URL];
    NSString *host = [url host];

    if ([challenge previousFailureCount])
    {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        
        NSLog(@"Cannot certificate %@", host);
    }
    else
    {
        //
        NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username"
                                                                 password:@"password"
                                                              persistence:NSURLCredentialPersistenceForSession];
        
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
        
        //
        NSLog(@"The %@ is certificated", host);
        
//        [[AppInfo sharedInstance] addCertificatedHost:host];
    }
}

@end
